// services/api.js
// 统一请求头
import axios from 'axios';

const api = axios.create({
    baseURL: 'http://localhost:8080', // 确保这个地址正确
    timeout: 1000000,
    headers: {
        'Content-Type': 'application/json',
    },
});

// 请求拦截器
api.interceptors.request.use(
    (config) => {
        const token = localStorage.getItem('token');
        if (token) {
            config.headers.Authorization = `Bearer ${token}`;
        }
        console.log('发送请求:', config.method.toUpperCase(), config.url, config.data); // 调试日志
        return config;
    },
    (error) => {
        return Promise.reject(error);
    }
);

// 响应拦截器
api.interceptors.response.use(
    (response) => {
        //console.log('收到响应:', response.status, response.data); // 调试日志
        return response;
    },
    (error) => {
        //console.error('请求错误:', error.response?.status, error.response?.data); // 调试日志
        return Promise.reject(error);
    }
);

export default api;
